package com.heatup.apt.api.controller;

import com.heatup.apt.api.service.OrderService;
import com.heatup.apt.common.constants.ErrorCodeConstants;
import com.heatup.apt.common.dao.OrderInfoMapper;
import com.heatup.apt.common.util.JsonTools;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.ResponseTools;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.order.OrderInfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * 
 * 订单
 * @author Jindaodaxia
 *
 */
@Controller
@RequestMapping("/order")
public class OrderController extends BaseController{

	public static final Logger log = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private OrderService orderService;
    
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    
    /*
     * 订单信息
     */
    @ResponseBody
    @RequestMapping(value = "/info", method = {RequestMethod.POST, RequestMethod.GET})
    private String info(HttpServletRequest request) {
        String callback=RequestTools.RequestString(request, "callback", "");
        int orderId = RequestTools.RequestInt(request,"orderId",0);
        
        ApiResultModel<Map<String,Object>> apiResultModel=new ApiResultModel<Map<String,Object>>();
        apiResultModel.setLogined(this.isLogined());    
        
        //未登陆
        if(!isLogined()){
        	apiResultModel.setRet(ApiResultModel.RET_ERROR);
        	apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
        	apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
        	return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
        }
  
        try{
        	apiResultModel.setData(orderService.getOrderInfo(orderId));
            apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
            apiResultModel.setMsg("");
        }catch(Exception e){
			e.printStackTrace();
			log.error(ErrorCodeConstants.Code.ORDER_ERROR_EXCEPTION.getMessage()+ e.toString() + e.getCause(),e);
            apiResultModel.setRet(ApiResultModel.RET_ERROR);
            apiResultModel.setErrorCode(ErrorCodeConstants.Code.ORDER_ERROR_EXCEPTION.getCode());
            apiResultModel.setMsg(ErrorCodeConstants.Code.ORDER_ERROR_EXCEPTION.getMessage() + e.toString() + e.getCause());
        }
       
        return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
    }   
    
    /**
     * 取消订单
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/cancel",method={RequestMethod.POST,RequestMethod.GET})
    public String cancelOrder(HttpServletRequest request){
    	 String callback=RequestTools.RequestString(request, "callback", ""); 
    	 int orderId = RequestTools.RequestInt(request,"orderId",0);
    	 int courseId=RequestTools.RequestInt(request, "courseId",0);
    	 
    	 ApiResultModel<Map<String,Object>> apiResultModel=new ApiResultModel<Map<String,Object>>();
         apiResultModel.setLogined(this.isLogined());    
		// 未登陆
		if (!isLogined()) {
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
			apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
		}
		try {
			if (orderId == 0) {
			    apiResultModel.setErrorCode(ErrorCodeConstants.Code.ORDER_ID_PARAM_WRONG.getCode());
				apiResultModel.setMsg(ErrorCodeConstants.Code.ORDER_ID_PARAM_WRONG.getMessage());
				apiResultModel.setRet(ApiResultModel.RET_ERROR);
			} else {
				if (orderService.cancelOrder(orderId, courseId)) {
					Map<String,Object> map = new HashMap<>();
					OrderInfo orderInfo = orderInfoMapper.selectById(orderId);
					map.put("cancelBackmoney", orderInfo.getCancelBackmoney());
					apiResultModel.setData(map);
					apiResultModel.setMsg("订单取消成功");
					apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
				}else{
				    apiResultModel.setErrorCode(ErrorCodeConstants.Code.ORDER_CANCEL_FAIL.getCode());
					apiResultModel.setMsg(ErrorCodeConstants.Code.ORDER_CANCEL_FAIL.getMessage());
					apiResultModel.setRet(ApiResultModel.RET_ERROR);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(ErrorCodeConstants.Code.ORDER_ERROR_EXCEPTION.getMessage() + e.toString() + e.getCause(),e);
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode() );
			apiResultModel.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage() +  e.toString() + e.getCause());
		}
         
         return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
    }
    
    /**
     * 查询该取消订单是否在可退款
     * @param request
     * @param response
     * @return
     */
	@ResponseBody
	@RequestMapping("/if_refund")
	public String ifRefund(HttpServletRequest request, HttpServletResponse response) {
		String callback = RequestTools.RequestString(request, "callback", "");
		int orderId = RequestTools.RequestInt(request, "orderId", 0);
		int courseId = RequestTools.RequestInt(request, "courseId", 0);
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		apiResultModel.setLogined(isLogined());
		try {
			apiResultModel.setData(orderService.ifRefund(orderId, courseId));
			apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
            log.error(ErrorCodeConstants.Code.ORDER_ERROR_EXCEPTION.getMessage() + e.toString() + e.getCause(),e);
            apiResultModel.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage() + e.toString() + e.getCause());
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
		}
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
	}
}
